﻿//
// GNS_PopWndPassWord.cpp
// MobileManageSys
//FuncTion 修改密码
// Created by mr.l on 16/1/13.
//
//
#include "GnsClientFrame.h"
#include "GNS_PopWndPassWord.h"
#include "cocostudio/CocoStudio.h"
#include "ui/CocosGUI.h"
#include "PopWndLayer.h"
#include "GNS_PlayerMgr.h"
#include "GNS_SceneMgr.h"
#include "SystemMgr.h"
#include "GnsOften.h"

USING_NS_CC;
using namespace cocostudio::timeline;
using namespace ui;

//extern ISocketMgr * _GameSock;
#define winsize Director::getInstance()->getVisibleSize()

GNS_PopWndPassWord::GNS_PopWndPassWord()
{
    pGNS_PopWndPassWord=0;
    oldPassWordText=0;
    newPassWordText=0;
    againPassWordText=0;
	_oldPassWordText = 0;
	_newPassWordText = 0;
	_againPassWordText = 0;
    determine=0;
}

GNS_PopWndPassWord::~GNS_PopWndPassWord()
{
    
}

GNS_PopWndPassWord* GNS_PopWndPassWord::createLayer()
{
    auto pPassWord = GNS_PopWndPassWord::create();
    auto pScene = Director::getInstance()->getRunningScene();
    if (!pScene)
        return 0;
     pScene->addChild(pPassWord);
    return pPassWord;
}

bool GNS_PopWndPassWord::init()
{
    if ( !Layer::init() )
    {
        return false;
    }
    return true;
}

void GNS_PopWndPassWord::SetNodeName(std::string CsbName)
{
    pGNS_PopWndPassWord = CSLoader::createNode(CsbName);
    pGNS_PopWndPassWord->setContentSize(winsize);
    ui::Helper::doLayout(pGNS_PopWndPassWord);
    addChild(pGNS_PopWndPassWord,11);
    GNS_PlayerMgr::getInstance()->_NodeChildVec.pushBack(pGNS_PopWndPassWord);
    
    auto pBackBtn = static_cast<Button*>(pGNS_PopWndPassWord->getChildByName("PassWordBackBtn"));
    pBackBtn->setTag(POPWNDBTN_TAG);
    pBackBtn->addTouchEventListener(CC_CALLBACK_2(GNS_PopWndPassWord::BtnCallBack, this));
	oldPassWordText = static_cast<cocos2d::ui::TextField *>(pGNS_PopWndPassWord->getChildByName("TextField_old"));
	newPassWordText = static_cast<cocos2d::ui::TextField *>(pGNS_PopWndPassWord->getChildByName("TextField_new"));
	againPassWordText = static_cast<cocos2d::ui::TextField *>(pGNS_PopWndPassWord->getChildByName("TextField_again"));
	
	//oldPassWordText->setPlaceHolder("请输入旧密码");
	//newPassWordText->setPlaceHolder("请输入新密码");
	//againPassWordText->setPlaceHolder("请再次输入新密码");
	
	oldPassWordText->setColor(Color3B::BLACK);
	newPassWordText->setColor(Color3B::BLACK);
	againPassWordText->setColor(Color3B::BLACK);

	_oldPassWordText = GNS_Handler::createEditBoxWithExistFeild(oldPassWordText, pGNS_PopWndPassWord, 14, EditBox::InputMode::EMAIL_ADDRESS, true, EditBox::InputFlag::PASSWORD);
	_newPassWordText = GNS_Handler::createEditBoxWithExistFeild(newPassWordText, pGNS_PopWndPassWord, 14, EditBox::InputMode::EMAIL_ADDRESS, true, EditBox::InputFlag::PASSWORD);
	_againPassWordText = GNS_Handler::createEditBoxWithExistFeild(againPassWordText, pGNS_PopWndPassWord, 14, EditBox::InputMode::EMAIL_ADDRESS, true, EditBox::InputFlag::PASSWORD);
	
	_oldPassWordText->setColor(Color3B::BLACK);
	_newPassWordText->setColor(Color3B::BLACK);
	_againPassWordText->setColor(Color3B::BLACK);

    determine = static_cast<Button*>(pGNS_PopWndPassWord->getChildByName("Button_2"));
    determine->setTag(POPWNDBTN_SEND);
    determine->addTouchEventListener(CC_CALLBACK_2(GNS_PopWndPassWord::BtnCallBack, this));

}
void GNS_PopWndPassWord::DetectionPassWord()
{
	//std::string oldstr = SystemMgr::getInstance()->utf8ToGb(oldPassWordText->getString().c_str());
	//std::string newstr = SystemMgr::getInstance()->utf8ToGb(newPassWordText->getString().c_str());
	//std::string againstr = SystemMgr::getInstance()->utf8ToGb(againPassWordText->getString().c_str());

	std::string oldstr = SystemMgr::getInstance()->utf8ToGb(_oldPassWordText->getText());
	std::string newstr = SystemMgr::getInstance()->utf8ToGb(_newPassWordText->getText());
	std::string againstr = SystemMgr::getInstance()->utf8ToGb(_againPassWordText->getText());

	int strLength = newstr.compare(againstr);
	int Isequal = oldstr.compare(newstr);
	//oldPassWordText->clear();
	// NewPassWord->clearUpText("");
	// AgainPassWord->clearUpText("");
	if (!CCGnsOften::checkPassWord(oldstr)) {
		auto PopWnd = PopWndLayer::createScene("Popwnd.png", "PageClose.png");
		PopWnd->SetTitleWithUTF("警告!");
		PopWnd->SetContentTextWithUTF("旧密码错误!,请重新输入!", Color3B(0, 0, 0));
		PopWnd->setClickWndOutsideClose(true);
	}
	else if (!CCGnsOften::checkPassWord(newstr)) {
		auto PopWnd = PopWndLayer::createScene("Popwnd.png", "PageClose.png");
		PopWnd->SetTitleWithUTF("警告!");
		PopWnd->SetContentTextWithUTF("新密码有误,请重新输入!", Color3B(0, 0, 0));
	}
	else if (!CCGnsOften::checkPassWord(againstr)) {
		auto PopWnd = PopWndLayer::createScene("Popwnd.png", "PageClose.png");
		PopWnd->SetTitleWithUTF("警告!");
		PopWnd->SetContentTextWithUTF("新密码有误,请重新输入!", Color3B(0, 0, 0));
	}
	else if (strLength != 0) {
		auto PopWnd = PopWndLayer::createScene("Popwnd.png", "PageClose.png");
		PopWnd->SetTitleWithUTF("警告!");
		PopWnd->SetContentTextWithUTF("两次输入的密码不同,请重新输入!", Color3B(0, 0, 0));
	}
	else if (Isequal == 0) {
		auto PopWnd = PopWndLayer::createScene("Popwnd.png", "PageClose.png");
		PopWnd->SetTitleWithUTF("警告!");
		PopWnd->SetContentTextWithUTF("新密码不能和旧密码相同,请重新输入!", Color3B(0, 0, 0));
	}
	else{
		CMD_CS_ChangePassword pPassWord;
		pPassWord.iUserID = (int)GNS_PlayerMgr::getInstance()->getMyUseridx();
		char PassWord[33] = { 0 };
		memcpy(PassWord, oldstr.c_str(), 33);
		memcpy(pPassWord.szOldPwd, SystemMgr::getInstance()->md5String(PassWord), 33);

		char NewPassWord[33] = { 33 };
		memcpy(NewPassWord, newstr.c_str(), 33);
		memcpy(pPassWord.szNewPwd, SystemMgr::getInstance()->md5String(NewPassWord), 33);
		//_GameSock->sendData(MAIN_MSG_LOGIN, SUB_CS_USER_CHANGE_PASSWORD, &pPassWord, sizeof(pPassWord));
		GnsClientFrame::getInstance()->sendMsg(0, MAIN_MSG_LOGIN, SUB_CS_USER_CHANGE_PASSWORD, (char*)&pPassWord, sizeof(pPassWord));
	}

}
void GNS_PopWndPassWord::BtnCallBack(cocos2d::Ref *pSender, ui::Widget::TouchEventType type){
    if (type==ui::Widget::TouchEventType::ENDED) {
        auto pButton = (Button*)pSender;
        int BtnTag = pButton->getTag();
        switch (BtnTag) {
            case POPWNDBTN_TAG:
            {
                if (GNS_PlayerMgr::getInstance()->_NodeChildVec.size()!=0) {
                     GNS_PlayerMgr::getInstance()->_NodeChildVec.clear();
                    this->removeAllChildrenWithCleanup(true);
                     this->getParent()->removeChild(this);
                   }
                //auto SystemNode=GNS_PlayerMgr::getInstance()->_NodeHallVec.at(0);
                //log("%zd",GNS_PlayerMgr::getInstance()->_NodeHallVec.size());
                //SystemNode->setVisible(true);

				GNS_NodeMgr::getInstance()->setMenuVisible("SystemMgr", true);
                break;
            }
            case POPWNDBTN_SEND:
            {
                DetectionPassWord();
                break;
            }
            default:
                break;
        }
    }
}
void GNS_PopWndPassWord::ClearDate()
{
	/*  oldPassWordText->setString("");
	  oldPassWord->setPlaceHolder("请输入旧密码");
	  NewPassWord->setString("");
	  NewPassWord->setPlaceHolder("请输入新密码");
	  AgainPassWord->setString("");
	  AgainPassWord->setPlaceHolder("请再次输入新密码");*/
}